At this stage of your
database engineering journey, you should have
experience of using JOIN operations to extract
data from multiple tables. There are also instances in
which you'll need to perform a JOIN operation on a MySQL
database using Python. In this video, you will
explore the process for executing JOIN
operations using Python. Little Lemon are adding a new menu feature
to their website. This feature lets
customers view menu items, their prices and the type of cuisine each meal is related to. The data that the
feature requires is in two different tables and
their MySQL database, menu and menu items. Little Lemon need to
use a JOIN to combine the data from these tables to
create their menu feature. Let's quickly recap
the concept of a JOIN. A JOIN is created using
the SQL JOIN clause. It targets a common column
between the two target tables. These common columns
are used to join the tables together and
extract the required records. Some examples of JOINs used
in MySQL include LEFT JOIN, RIGHT JOIN, INNER
JOIN and OUTER JOIN. All these JOINs can
be used with Python. Let's explore an
example of the syntax and process for
creating an INNER JOIN. First, a SQL query
is created using JOIN as a Python string that
can be passed to a variable. The query is then run using the execute method from
the cursor object. Once the query is executed, you can retrieve its results
into another variable. Use the fetchall method on the cursor object that holds
the results of the JOIN. The data is retrieved
as a list of tuples. For example, Little Lemon
can create a JOIN query as a string object in
Python that combines the menu items and menu tables. This join is performed
using an INNER JOIN on the item ID column which
is common to both tables. When executed using Python
on a MySQL database, this statement returns
all required results. Now that you're familiar
with the process for joining data from different
tables using Python, let's see if you can help Little Lemon to
create this query. As you learned earlier, Little Lemon need to
use a JOIN query to extract the data for the new menu feature
on their website. They begin by creating the join statement as a
string object in Python. Store it in a variable
called my join query. The join query must
target the name, type and price columns
from the menu items table. And it must also target the cuisine column
from the menu table. An INNER JOIN is created on the item ID column which
is common to both tables. Let's assume that the
MySQL Connector python API is running a connection between the front and back-end and that the cursor
object is also available. This means that you
can now execute your query using
the JOIN statement. Run the query using the execute method from
the cursor object. When the query is executed, you can fetch all the results
from the cursor object in a new variable called results
using the fetchall method. The results are retrieved
as a list of tuples, one for each row. Little Lemon can view the order
of each individual entry. They just call the column
names attribute on the cursor to return the
names of the column in order. At this stage of your
database engineering journey, you should have
experience of using JOINS to extract data from
multiple tables. And you should now be familiar
with extracting data from a MySQL database using JOINS
and Python. Well done.